package com.sky.myapplication.kotlin.weight

import android.content.Context
import android.graphics.*
import android.view.View

class MyView: View{

    lateinit var mpts: FloatArray
    val SIZE: Float = 300.0f
    val SEGS: Int = 32
    val X: Int = 0
    val Y: Int = 1

    constructor(context: Context): super(context){
        buildPoints()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        //使用Canvas绘图
        //画布移动到(10,10)位置
        
        canvas?.translate(10f, 10f)
        canvas?.drawColor(Color.WHITE)
        //创建红色画笔，使用单像素宽度，绘制直线
        var paint = Paint()
        paint.setColor(Color.RED)
        paint.setStrokeWidth(0f)
        canvas?.drawLines(mpts, paint)
        //创建蓝色画笔，宽度为3，绘制相关点
        paint.setColor(Color.BLUE)
        paint.setStrokeWidth(3f)
        canvas?.drawPoints(mpts, paint)
        //创建Path, 并沿着path显示文字信息
        var rect = RectF(10f, 300f, 290f, 430f)
        var path = Path()
        path.addArc(rect, -180f, 180f)
        paint.setTextSize(18f)
        paint.setColor(Color.BLUE)
        canvas?.drawTextOnPath("在自定义View中使用Canvas对象绘图实例", path, 0f, 0f, paint)
    }

    private fun buildPoints(){
        val ptCount = (SEGS + 1) * 2
        mpts = FloatArray(ptCount * 2)

        var value = 0f
        val delta = SIZE / SEGS

        for (index in 0..SEGS){
            mpts.set((index * 4 + x).toInt(), SIZE - value)
            mpts.set((index * 4 + Y).toInt(), 0f)
            mpts.set((index * 4 + X + 2).toInt(), 0f)
            mpts.set((index * 4 + Y + 2).toInt(), value.toFloat())
            value += delta
        }

    }
}